//
//  DownDetailsViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/12.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DownDetailsViewController.h"
#import "GetDataMethods.h"
#import "UIImageView+WebCache.h"
#import "Waypoints.h"
#import "WaypointsTableViewCell.h"
#import "Model.h"
#import "Fit.h"
#import "MBProgressHUD.h"

@interface DownDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *daysArray;
@property(nonatomic, retain)NSMutableArray *dateArray;
@property(nonatomic, retain)NSDictionary *allDic;
@property(nonatomic, retain)NSMutableArray *waypointsArray;
@property(nonatomic, retain)MBProgressHUD *progressView;
@property(nonatomic, retain)UIView *headView;
@property(nonatomic, retain)UILabel *userLabel;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *timeLabel;
@property(nonatomic, retain)UILabel *tianLabel;
@property(nonatomic, retain)UILabel *kmLabel;
@property(nonatomic, retain)UILabel *mileageLabel;
@property(nonatomic, retain)UILabel *likeLabel;
@property(nonatomic, retain)UILabel *numLabel;
@end

@implementation DownDetailsViewController
-(void)dealloc
{
    [_tableView release];
    [_daysArray release];
    [_allDic release];
    [_dateArray release];
    [_waypointsArray release];
    [_headView release];
    [_userLabel release];
    [_nameLabel release];
    [_tianLabel release];
    [_timeLabel release];
    [_kmLabel release];
    [_mileageLabel release];
    [_likeLabel release];
    [_numLabel release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    self.headView.backgroundColor = [Model shareInstance].BackgroundColor;
    self.nameLabel.textColor = [Model shareInstance].TitleColor;
    self.userLabel.textColor = [Model shareInstance].TitleColor;
    self.timeLabel.textColor = [Model shareInstance].TitleColor;
    self.tianLabel.textColor = [Model shareInstance].TitleColor;
    self.mileageLabel.textColor = [Model shareInstance].TitleColor;
    self.kmLabel.textColor = [Model shareInstance].TitleColor;
    self.likeLabel.textColor  = [Model shareInstance].TitleColor;
    self.numLabel.textColor = [Model shareInstance].TitleColor;
    
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self changeColor];
}

-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    [self getData];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}

-(void)getData
{
    self.daysArray = [NSMutableArray array];
    self.dateArray = [NSMutableArray array];
    self.allDic = [NSDictionary dictionary];
    self.waypointsArray = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", _string];
    [GetDataMethods getDataByString:str BodyString:nil WithDataBlock:^(id data) {
        _allDic = data;
        NSArray *dayArray = [data objectForKey:@"days"];
        for (NSDictionary *dayDic in dayArray) {
            [_daysArray addObject:[dayDic objectForKey:@"day"]];
            [_dateArray addObject:[dayDic objectForKey:@"date"]];
            NSArray *wayArray = [dayDic objectForKey:@"waypoints"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *wayDic in wayArray) {
                Waypoints *point = [[Waypoints alloc]init];
                [point setValuesForKeysWithDictionary:wayDic];
                [array addObject:point];
            }
            [_waypointsArray addObject:array];
        }
        [_tableView reloadData];
        [self createViews];
        [_progressView removeFromSuperview];
    }];
}



-(void)createViews
{
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 520 * H)];
    _tableView.tableHeaderView = _headView;
    [_headView release];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * H)];
    if ([_allDic objectForKey:@"trackpoints_thumbnail_image"]) {
        NSString *string = [NSString stringWithFormat:@"%@", [_allDic objectForKey:@"trackpoints_thumbnail_image"]];
        [imageView sd_setImageWithURL:[NSURL URLWithString:string]];
    }
    [_headView addSubview:imageView];
    [imageView release];
    UIImageView *userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(150 * W, 165 * H, 70 * W, 70 * W)];
    [userImageView sd_setImageWithURL:[NSURL URLWithString:[[_allDic objectForKey:@"user"] objectForKey:@"avatar_l"]]];
    userImageView.layer.cornerRadius = userImageView.frame.size.width / 2;
    userImageView.layer.masksToBounds = YES;
    userImageView.layer.borderWidth = 2;
    userImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    [_headView addSubview:userImageView];
    [userImageView release];
    self.userLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * W, userImageView.frame.origin.y + userImageView.frame.size.height + 15 * H, _headView.frame.size.width - 40 * W, 20 * H)];
    _userLabel.text = [NSString stringWithFormat:@"by %@", [[_allDic objectForKey:@"user"] objectForKey:@"name"]];
    _userLabel.font = [UIFont systemFontOfSize:15];
    _userLabel.textAlignment = 1;
    [_headView addSubview:_userLabel];
    [_userLabel release];
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_userLabel.frame.origin.x, _userLabel.frame.origin.y + _userLabel.frame.size.height + 10 * H, _userLabel.frame.size.width, 50 * H)];
    _nameLabel.text = [_allDic objectForKey:@"name"];
    _nameLabel.font = [UIFont systemFontOfSize:18];
    _nameLabel.numberOfLines = 0;
    _nameLabel.textAlignment = 1;
    [_headView addSubview:_nameLabel];
    [_nameLabel release];
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10 * H, _nameLabel.frame.size.width, 2 * H)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [_headView addSubview:lineLabel];
    [lineLabel release];
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(lineLabel.frame.origin.x, lineLabel.frame.origin.y + lineLabel.frame.size.height + 5 * H, 111 * W, 20 * H)];
    _timeLabel.text = [_allDic objectForKey:@"first_day"];
    _timeLabel.font = [UIFont systemFontOfSize:13];
    _timeLabel.textAlignment = 1;
    [_headView addSubview:_timeLabel];
    [_timeLabel release];
    self.tianLabel = [[UILabel alloc]initWithFrame:CGRectMake(_timeLabel.frame.origin.x, _timeLabel.frame.origin.y + _timeLabel.frame.size.height, _timeLabel.frame.size.width, _timeLabel.frame.size.height)];
    _tianLabel.text = [NSString stringWithFormat:@"%@天", [_allDic objectForKey:@"day_count"]];
    _tianLabel.font = [UIFont systemFontOfSize:13];
    _tianLabel.textAlignment = 1;
    [_headView addSubview:_tianLabel];
    [_tianLabel release];
    UILabel *xianLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(_timeLabel.frame.origin.x + _timeLabel.frame.size.width, _timeLabel.frame.origin.y, 1 * W, _timeLabel.frame.size.height + _tianLabel.frame.size.height)];
    xianLabel1.backgroundColor = [UIColor grayColor];
    [_headView addSubview:xianLabel1];
    [xianLabel1 release];
    self.mileageLabel = [[UILabel alloc]initWithFrame:CGRectMake(xianLabel1.frame.origin.x + xianLabel1.frame.size.width, xianLabel1.frame.origin.y, _timeLabel.frame.size.width, _timeLabel.frame.size.height)];
    _mileageLabel.text = @"里程";
    _mileageLabel.font = [UIFont systemFontOfSize:13];
    _mileageLabel.textAlignment = 1;
    [_headView addSubview:_mileageLabel];
    [_mileageLabel release];
    self.kmLabel = [[UILabel alloc]initWithFrame:CGRectMake(_mileageLabel.frame.origin.x, _mileageLabel.frame.origin.y + _mileageLabel.frame.size.height, _mileageLabel.frame.size.width, _mileageLabel.frame.size.height)];
    CGFloat a = [[_allDic objectForKey:@"mileage"] floatValue];
    _kmLabel.text = [NSString stringWithFormat:@"%.2f万km", a / 10000];
    _kmLabel.font = [UIFont systemFontOfSize:13];
    _kmLabel.textAlignment = 1;
    [_headView addSubview:_kmLabel];
    [_kmLabel release];
    UILabel *xianLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(_mileageLabel.frame.origin.x + _mileageLabel.frame.size.width, xianLabel1.frame.origin.y, xianLabel1.frame.size.width, xianLabel1.frame.size.height)];
    xianLabel2.backgroundColor = [UIColor grayColor];
    [_headView addSubview:xianLabel2];
    [xianLabel2 release];
    self.likeLabel = [[UILabel alloc]initWithFrame:CGRectMake(xianLabel2.frame.origin.x + xianLabel2.frame.size.width, _mileageLabel.frame.origin.y, _mileageLabel.frame.size.width, _mileageLabel.frame.size.height)];
    _likeLabel.text = @"喜欢";
    _likeLabel.font = [UIFont systemFontOfSize:13];
    _likeLabel.textAlignment = 1;
    [_headView addSubview:_likeLabel];
    [_likeLabel release];
    self.numLabel = [[UILabel alloc]initWithFrame:CGRectMake(_likeLabel.frame.origin.x, _likeLabel.frame.origin.y + _likeLabel.frame.size.height, _likeLabel.frame.size.width, _likeLabel.frame.size.height)];
    NSInteger b = [[_allDic valueForKey:@"recommendations"] integerValue];
    _numLabel.text = [NSString stringWithFormat:@"%ld", b];
    _numLabel.font = [UIFont systemFontOfSize:13];
    _numLabel.textAlignment = 1;
    [_headView addSubview:_numLabel];
    [_numLabel release];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(_tianLabel.frame.origin.x, _tianLabel.frame.origin.y + _tianLabel.frame.size.height + 10 * H, _nameLabel.frame.size.width, 100 * H)];
    backView.backgroundColor = [UIColor whiteColor];
    [_headView addSubview:backView];
    [backView release];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, backView.frame.size.width, 4 * H)];
    lineView.backgroundColor = [UIColor purpleColor];
    [backView addSubview:lineView];
    [lineView release];
    UIImageView *flyImageview = [[UIImageView alloc]initWithFrame:CGRectMake(10 * W, 15 * H, 30 * W, 30 * H)];
    flyImageview.image = [UIImage imageNamed:@"hangban"];
    [backView addSubview:flyImageview];
    [flyImageview release];
    UILabel *flyLabel = [[UILabel alloc]initWithFrame:CGRectMake(flyImageview.frame.origin.x + flyImageview.frame.size.width + 10 * W, flyImageview.frame.origin.y, 100 * W, flyImageview.frame.size.height)];
    flyLabel.text = [NSString stringWithFormat:@"%@ 航班", [[_allDic objectForKey:@"poi_infos_count"] objectForKey:@"flight"]];
    flyLabel.font = [UIFont systemFontOfSize:15];
    [backView addSubview:flyLabel];
    [flyLabel release];
    UIImageView *hotelImageView = [[UIImageView alloc]initWithFrame:CGRectMake(flyLabel.frame.origin.x + flyLabel.frame.size.width + 10 * W, flyLabel.frame.origin.y, flyImageview.frame.size.width, flyImageview.frame.size.height)];
    hotelImageView.image = [UIImage imageNamed:@"lvdian"];
    [backView addSubview:hotelImageView];
    [hotelImageView release];
    UILabel *hotelLabel = [[UILabel alloc]initWithFrame:CGRectMake(hotelImageView.frame.origin.x + hotelImageView.frame.size.width + 10 * W, hotelImageView.frame.origin.y, flyLabel.frame.size.width, flyLabel.frame.size.height)];
    hotelLabel.text = [NSString stringWithFormat:@"%@ 旅店", [[_allDic objectForKey:@"poi_infos_count"] objectForKey:@"hotel"]];
    hotelLabel.font = [UIFont systemFontOfSize:15];
    [backView addSubview:hotelLabel];
    [hotelLabel release];
    UIImageView *fanImageView = [[UIImageView alloc]initWithFrame:CGRectMake(flyImageview.frame.origin.x, flyImageview.frame.origin.y + flyImageview.frame.size.height + 10 * H, flyImageview.frame.size.width, flyImageview.frame.size.height)];
    fanImageView.image = [UIImage imageNamed:@"canting"];
    [backView addSubview:fanImageView];
    [fanImageView release];
    UILabel *fanLabel = [[UILabel alloc]initWithFrame:CGRectMake(fanImageView.frame.origin.x + fanImageView.frame.size.width + 10 * W, fanImageView.frame.origin.y, flyLabel.frame.size.width, flyLabel.frame.size.height)];
    fanLabel.text = [NSString stringWithFormat:@"%@ 餐厅", [[_allDic objectForKey:@"poi_infos_count"] objectForKey:@"restaurant"]];
    fanLabel.font = [UIFont systemFontOfSize:15];
    [backView addSubview:fanLabel];
    [fanLabel release];
    UIImageView *buyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(hotelImageView.frame.origin.x, hotelImageView.frame.origin.y + hotelImageView.frame.size.height + 10 * H, hotelImageView.frame.size.width, hotelImageView.frame.size.height)];
    buyImageView.image = [UIImage imageNamed:@"gouwu"];
    [backView addSubview:buyImageView];
    [buyImageView release];
    UILabel *buyLabel = [[UILabel alloc]initWithFrame:CGRectMake(hotelLabel.frame.origin.x, fanLabel.frame.origin.y, hotelLabel.frame.size.width, hotelLabel.frame.size.height)];
    buyLabel.text = [NSString stringWithFormat:@"%@ 购物", [[_allDic objectForKey:@"poi_infos_count"] objectForKey:@"mall"]];
    buyLabel.font = [UIFont systemFontOfSize:15];
    [backView addSubview:buyLabel];
    [buyLabel release];
    
    [self changeColor];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _daysArray.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"第%@天 %@", _daysArray[section], _dateArray[section]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * H * 1.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    WaypointsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[WaypointsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_waypointsArray.count != 0) {
        cell.point = _waypointsArray[indexPath.section][indexPath.row];
        [cell setPhoto_info:[_waypointsArray[indexPath.section][indexPath.row]photo_info]];
    }
    cell.indexLabel.textColor = [Model shareInstance].TitleColor;
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    cell.timeLabel.textColor = [Model shareInstance].TitleColor;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_waypointsArray[section] count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_waypointsArray.count != 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        CGRect rect = [[_waypointsArray[indexPath.section][indexPath.row] text ]boundingRectWithSize:CGSizeMake(335 * W, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        CGFloat width = [[[_waypointsArray[indexPath.section][indexPath.row]photo_info] objectForKey:@"w"]floatValue];
        CGFloat height = [[[_waypointsArray[indexPath.section][indexPath.row]photo_info]objectForKey:@"h"]floatValue];
        CGFloat bili = width / 335.0;
        if (bili != 0) {
            CGFloat imageHeight = height / bili;
            return (CGFloat)(imageHeight + ceill(rect.size.height + 1) + 60 * H) * 1.0;
        }
    }
    return 0.0;
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
